<?php

/**
 * Class Home
 *
 * Please note:
 * Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
 * This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
 *
 */

class Contactus extends Controller
{
    /**
     * PAGE: index
     * This method handles what happens when you move to http://yourproject/home/index (which is the default page btw)
     */
    public function index()
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$count = $laptop_model->countLaptops();
    	$randid = rand(1,$count[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	$info = "";
        // debug message to show where you are, just for the demo
        // echo 'Message from Controller: You are in the controller home, using the method index()';
        require 'application/views/common/_header.php';
        require 'application/views/contactus/index.php';
        require 'application/views/common/_footer.php';
    }
    
    public function sendmail()
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$count = $laptop_model->countLaptops();
    	$randid = rand(1,$count[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	
    	
    	if($_SERVER["REQUEST_METHOD"] == "POST" && $_POST['enquiremail'] != "" && $_POST['enquiry'] != "" && $_POST['enquirename'] != ""){

     		$mail = new PHPMailer();
     		
    		$subject = "Laptop Enquiry From : ".$_POST['enquirename'];
    		$body = $subject."\r\nEnquire Email Address :".$_POST['enquiremail']."\r\n\r\n".$_POST['enquiry'];
    		 
    		$mail->IsSMTP();
    		$mail->SMTPAuth = true;
    		$mail->Host = SMTP_SERVER;
    		$mail->Port = SMTP_PORT;
    		//$mail->SMTPSecure = 'tls'; //comment this when using server mail client In Production
    		$mail->Username = SMTP_USER;
    		$mail->Password = SMTP_PASS;
    		//$mail->SMTPDebug = 2;
    		//$mail->Debugoutput = 'html';
    		 
    		$mail->SetFrom(SMTP_EMAILFROM, 'DoerIT Laptops');
    		$mail->Subject = $subject;
    		$mail->MsgHTML($body);
    		$mail->AddAddress(SMTP_EMAILTO, $_POST['enquirename']);
    		
    		 if($mail->Send()) {
    			$info = "<h1 style='color:green;'>Email Sent Successfully!!</h1>";
    			
    			$enquiry_model = $this->loadModel('EnquiryModel');
    			$enquiry_model->addAnEnquiry($_POST['enquirename'],$_POST["enquiremail"],"",$_POST['enquiry'],"LAPTOPSITE");
    			
    		} else {
    			//echo "Mailer Error: " . $mail->ErrorInfo;
    			$info = "<h1 style='color:red;'>Email Sending Failed, Please Try Again Later!!</h1>";
    		}  	     	

	    	require 'application/views/common/_header.php';
	    	require 'application/views/contactus/_mailsent.php';
	    	require 'application/views/common/_footer.php';
	    	
    	} else {
    		
    		$info = "<h2 style='color:red;'>Please Fill the Requierd Fields!!</h2>";
    		
    		require 'application/views/common/_header.php';
    		require 'application/views/contactus/index.php';
    		require 'application/views/common/_footer.php';
    	}
    	
    }
    
}
